package com.qlm.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.qlm.entity.PrizesConfig;
import com.qlm.entity.Yjjf;
import com.qlm.entity.Yscore;
import com.qlm.interceptor.AdminOperateInterceptor;
import com.qlm.interceptor.AuthInterceptor;
import com.qlm.service.IPrizesService;
import com.qlm.service.IYyyScoreService;
import com.qlm.service.impl.PrizesServiceImpl;
import com.qlm.service.impl.YyyScoreServiceImpl;
import com.qlm.tools.Common;
import com.qlm.view.ResultMessage;

/**
 * 摇一摇奖品配置管理
 * @author Administrator
 *
 */
@Before({AuthInterceptor.class , AdminOperateInterceptor.class})
public class PrizesController extends Controller {
	private static Logger logger = LoggerFactory.getLogger(PrizesController.class);
	
	private IPrizesService prizesService = enhance(PrizesServiceImpl.class);
	private IYyyScoreService yyyScoreService = enhance(YyyScoreServiceImpl.class);
	
	/**
	 * 进入奖品配置页面
	 */
	public void getPrizesConfig(){
		Integer type = getParaToInt("type");
//		List<PrizesConfig> list = prizesService.getPrizesConfig(type);
//		setAttr("prizesList", list);
		setAttr("type", type);
		renderJsp("/page/yyyConfig.jsp");
	}
	
	/**
	 * 中奖记录
	 */
	public void prizesRecord(){
		String aoData = getPara("aoData");
		  if(aoData == null){
			  render("/page/yyyPrizesRecord.jsp");
			  return;
		  }else{
			  String  jsonStr = prizesService.getPrizesRecord(aoData);
			  renderJson(jsonStr);
		  }
	}
	
	
	/**
	 * 获取帮摇积分配置
	 */
	public void getScoreConfig(){
		List<Yscore> list = yyyScoreService.getScoreConfig();
		Yjjf yjjf = yyyScoreService.getYjjf();
		setAttr("scoreList", list);
		setAttr("yjjf", yjjf);
		renderJsp("/page/yyyScore.jsp");
	}
	
	/**
	 * 保存积分配置
	 */
	public void saveScoreConfig(){
		Integer[] score_ = getParaValuesToInt("score");
		String[] rate_ = getParaValues("rate");
		ResultMessage rm = yyyScoreService.saveScoreConfig(score_, rate_);
		renderJson(rm);
	}
	
	/**
	 * 保存摇奖消耗积分
	 */
	public void saveYjjf(){
		Integer score = getParaToInt("score_");
		ResultMessage rm = yyyScoreService.saveYjjf(score);
		renderJson(rm);
	}

	/**
	 * 依据奖品类型查找奖品信息
	 */
	public void findYYYConfig(){
		Integer type = getParaToInt("type");
		ResultMessage rm = prizesService.findYYYConfig(type);
		renderJson(rm);
	}
	
	/**
	 * 依据ID查找奖品信息
	 */
	public void findPrizesById(){
		Integer id = getParaToInt("id");
		ResultMessage rm = prizesService.findPrizes(id);
		renderJson(rm);
	}
	
	/**
	 * 修改奖品信息
	 */
	public void savePrizes(){
		ResultMessage rm ;
		String id = getPara("id");
		String name = getPara("name");
		String num = getPara("num");
		String amount = getPara("amount");
		String type = getPara("type");
		String rate = getPara("rate");
		if(!Common.isEmptyString(name)){
			rm = new ResultMessage(0, "奖品名称不能为空", null);
		}else if(!Common.isEmptyString(num)){
			rm = new ResultMessage(0, "奖品数量不能为空", null);
		}else if(!Common.isEmptyString(amount)){
			rm = new ResultMessage(0, "奖品金额(积分)不能为空", null);
		}else if(!Common.isEmptyString(type)){
			rm = new ResultMessage(0, "奖品类型不能为空", null);
		}else if(!Common.isEmptyString(rate)){
			rm = new ResultMessage(0, "中奖概率不能为空", null);
		}else if(!Common.isEmptyString(id)){
			rm = new ResultMessage(0, "网络连接错误", null);
		}
		PrizesConfig prizes = new PrizesConfig();
		prizes.set("name_", name);
		prizes.set("num_", getParaToInt("num"));
		prizes.set("id", getParaToInt("id"));
		prizes.set("amount_", getPara("amount"));
		prizes.set("type_", getParaToInt("type"));
		prizes.set("rate_", rate);
		rm = prizesService.savePrizes(prizes);
		renderJson(rm);
	}
	
	public void addPrizes(){
		ResultMessage rm ;
		String name = getPara("name");
		String num = getPara("num");
		String jf = getPara("jf");
		String hb = getPara("hb");
		String type = getPara("type");
		String rate = getPara("rate");
		if(!Common.isEmptyString(name)){
			rm = new ResultMessage(0, "奖品名称不能为空", null);
		}else if(!Common.isEmptyString(num)){
			rm = new ResultMessage(0, "奖品数量不能为空", null);
		}else if(!Common.isEmptyString(type)){
			rm = new ResultMessage(0, "奖品类型不能为空", null);
		}else if(!Common.isEmptyString(rate)){
			rm = new ResultMessage(0, "中奖概率不能为空", null);
		}
		PrizesConfig prizes = new PrizesConfig();
		prizes.set("name_", name);
		prizes.set("num_", getParaToInt("num"));
		prizes.set("type_", getParaToInt("type"));
		prizes.set("rate_", rate);
		rm = prizesService.addPrizes(prizes,jf,hb);
		renderJson(rm);
	}
	
	/**
	 * 依据ID删除奖品
	 */
	public void delPrizesById(){
		Integer id = getParaToInt("id");
		ResultMessage rm = prizesService.delPrizesById(id);
		renderJson(rm);
	}
}
